<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.util.logging.SimpleFormatter"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*, java.text.*" %>
<%@ page import="com.google.appengine.api.users.*" %>
<%@ page import="com.google.appengine.api.datastore.*" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>    
<%
	int projectId = request.getParameter("projectId")==null?201207:Integer.parseInt(request.getParameter("projectId"));	//프로젝트 ID	
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Key issueKey = KeyFactory.createKey("issue", projectId);
    
	/*    	
	Query query = new Query("issue", issueKey).addSort("frstRegistPnttm", Query.SortDirection.DESCENDING);
    List<Entity> issueList = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5));
    if (issueList==null) {
    	out.println("<p>no messages.</p>");
    } else {
        for (Entity issue : issueList) {
        	Key key = issue.getKey();
        	out.println(key.getId());
        	out.println(" | <a href='issueDetail.jsp?projectId=" + projectId + "&issueId=" + key.getId() + "'>");
        	out.println(issue.getProperty("issueSj"));
        	out.println("</a> | ");
        	out.println(((User)issue.getProperty("frstRegisterUser")).getNickname());
        	out.println(" | ");
        	out.println(formatter.format((Date)issue.getProperty("frstRegistPnttm")));
        	out.println("<br>");
        }
    } 
    */
    
    //페이징 처리
    int PAGE_SIZE = 10;
    
    PreparedQuery queryString = datastore.prepare(new Query("issue", issueKey).addSort("frstRegistPnttm", Query.SortDirection.DESCENDING));
    int rowCount = queryString.countEntities();
    
  	//set offset for the results to be fetched
    String off = request.getParameter("offset");
    int offset = 0;
    if (off != null)
    	offset = Integer.parseInt(off);

    //fetch results from datastore based on offset and page size
    FetchOptions fetchOptions = FetchOptions.Builder.withLimit(PAGE_SIZE);
    QueryResultList<Entity> results = queryString.asQueryResultList(fetchOptions .offset(offset));
%>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta http-equiv="X-UA-Compatible" content="chrome=1">
	<link href="http://www.gstatic.com/codesite/ph/17444577587916266307/css/ph_core.css" rel="stylesheet" type="text/css" />
	<link href="http://code.google.com/css/codesite.pack.04102009.css" rel="stylesheet" type="text/css" />
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
	<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/jquery-ui.min.js"></script>
	<title>이슈 목록</title>
</head>
<body>
<% if (user != null) { %>
<p>Hello, <%=user.getNickname()%>! (You can
<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign out</a>.)</p>
<%
    } else {
%>
<p>Hello!
<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Sign in</a>
to include your name with greetings you post.</p>
<%
    }
%>
	<!-- content -->
	<div id="gc-pagecontent">
		<h1 class="page_title">신재생 에너지 코리아</h1>

		<!-- tabs -->
		<div id="tabs" class="gtb">
			<a href="#" class="tab">Home</a> 
			<a href="#" class="tab active">이슈 관리</a> 
			<a href="#" class="tab">Product</a> 
			<a href="#" class="tab">Item</a> 
			<a href="#" class="tab">Order</a>
			<div class="gtbc"></div>
		</div>
		<!-- home page content -->
		<div class="g-unit">
			<!-- search container -->
		   	<div class="gsc-search-box" id="customer-search-ctr">
		   	<!-- section title -->
		   	<h2>이슈 목록</h2>
		   	<form name="customer-search-form" id="customer-search-form">
		   	<label>키워드</label>
	   	 		<input type="text" name="q" id="q" class="gsc-input"/> 
		   	    <input type="button" value="검색" onclick="search('customer')" class="gsc-search-button" />
	            <input type="button" value="등록" onclick="location.href='issueForm.jsp?projectId=<%=projectId%>'" class="gsc-search-button" />
	            <input type="reset" id="customer-search-reset" class="cancel" title="Reset" value="Reset" style="visibility: hidden"/>
		    </form>
		   	</div>
		   	
		   	<!-- list container -->
		   	<div class="results" style="border:0;" id="customer-list-ctr">
				<table width="100%" cellspacing="0" cellpadding="2" border="0" style="border-collapse:collapse;">
					<thead>
						<tr>
							<th scope="col">번호</th>
							<th scope="col">상태</th>
							<th scope="col">제목</th>
							<th scope="col">작성자</th>
							<th scope="col">작성일</th>
							<th scope="col">조회</th>
						</tr>
					</thead>
		            <tbody id="customer-list-tbody">
<%
  	//set the content of the table
  	String pageContent = "<tbody>";
    for (Entity entity : results) {
    	long issueId = entity.getKey().getId();
    	String issueSj = (String) entity.getProperty("issueSj");
    	User frstRegisterUser = (User)entity.getProperty("frstRegisterUser");
%>
						<tr>
							<td><%=issueId%></td>
							<td>등록</td>
							<td><a href='issueDetail.jsp?projectId=<%=projectId %>&issueId=<%=issueId %>'><%=issueSj %></a></td>
							<td><%=frstRegisterUser.getNickname()%></td>
							<td><%=formatter.format((Date)entity.getProperty("frstRegistPnttm"))%></td>
							<td>0</td>
						</tr>
<%
    }
    if (results.isEmpty()) {
   	 	// condition to show message when data is not available
    	pageContent += "<tbody><tr><td colspan=6>No records found</td></tr>";
    }
  	//set footer for the table              
    float pageCount = (float) rowCount / PAGE_SIZE;
    String footer = "";
    if (pageCount > rowCount / PAGE_SIZE)
            pageCount = (int) pageCount + 1;
    else
            pageCount = (int) pageCount;
    for (int i = 0; i < pageCount; i++) {
            footer += "<a href=\"issueList.jsp?projectId=" + projectId + "&offset=" + i * PAGE_SIZE + "\">" + ( i + 1 ) + "</a> " ;
    }
    if (rowCount > 10) {
    if (( offset / 10 ) != ( pageCount - 1 ))  
            footer += "<a href=\"issueList.jsp?projectId=" + projectId + "&offset=" + ( offset + 10 ) + "\">Next</a>" ;  
    else  
            footer += "<a href=\"issueList.jsp?projectId=" + projectId + "&offset=" + ( pageCount - 1 ) * PAGE_SIZE + "\">Next</a>" ;
    }
%>
						
		            </tbody>
		            <tfoot>
						<tr><td colspan=6 class="id" style="text-align:right"><%=footer%></td></tr>
					</tfoot>
				</table>
		  	</div>
		</div>
	</div>
</body>
</html>